#include "game.h"
#include "resources.h"

Game::Game(){

    _score = 0;
    _stage = 0;
    //_gb = new GameBoard();

    load_stage(_stage);
    _renderImg = al_create_bitmap(DISPLAY_W, DISPLAY_H);
    _sideMenuBMP = al_create_bitmap(DISPLAY_W-480, DISPLAY_H);
}

ALLEGRO_BITMAP* Game::renderSideMenu(){
    ALLEGRO_BITMAP* lastBmp = al_get_target_bitmap();
    al_set_target_bitmap(_sideMenuBMP);
    al_clear_to_color( al_map_rgba(0,0,0,180) );

    al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO), al_map_rgb(255,255,255), 80,0, ALLEGRO_ALIGN_CENTRE, "Super Mario");
    al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO), al_map_rgb(255,255,255), 80,30, ALLEGRO_ALIGN_CENTRE, "Sokoban");

    char cp_stage[8];
    char cp_score[8];

    std::string str_stage = std::string("Stage: ") + itoa(_stage,cp_stage,10);
    std::string str_moves = std::string("Moves: ") + itoa(_score,cp_score,10);

    al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO), al_map_rgb(255,255,255), 80,100, ALLEGRO_ALIGN_CENTRE, str_stage.c_str());
    al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO), al_map_rgb(255,255,255), 80,130, ALLEGRO_ALIGN_CENTRE, str_moves.c_str());

    al_set_target_bitmap(lastBmp);

    return _sideMenuBMP;
}

ALLEGRO_BITMAP* Game::render(){
    ALLEGRO_BITMAP* lastBmp = al_get_target_bitmap();
    al_set_target_bitmap(_renderImg);

    if( _state==GAME ){
        al_draw_bitmap(Resources::instance()->image( Resources::BACKGROUND ),0,0,0);
        al_draw_bitmap(_gb->render(), DISPLAY_W - (BLOCK_W * BOARD_COLS), 0, 0);
        al_draw_bitmap(renderSideMenu(),0,0,0);
    }else if( _state==END_GAME ){
        al_draw_bitmap(Resources::instance()->image( Resources::BACKGROUND ),0,0,0);
        al_draw_bitmap(_gb->render(), DISPLAY_W - (BLOCK_W * BOARD_COLS), 0, 0);
        al_draw_bitmap(renderSideMenu(),0,0,0);

        al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO),al_map_rgb(255,255,255), 300,230, ALLEGRO_ALIGN_CENTRE, "Fim de Jogo. Nao existem mais fases.");
        al_draw_text( Resources::instance()->font(Resources::FONT_SUPER_MARIO),al_map_rgb(255,255,255), 300,260, ALLEGRO_ALIGN_CENTRE, "Use o editor para construir mais!");
    }

    al_set_target_bitmap(lastBmp);

    return _renderImg;
}

void Game::check_stage_complete(){

    if(_gb->check_stage_complete()){
       // std:: cout << "STAGE COMPLETE" << std::endl;
        change_state(NEXT_STAGE);
    }

}

void Game::change_state(int new_state){
    _state = new_state;
    //std::cout<< new_state;
    if( new_state == NEXT_STAGE ){
        try{
            load_stage(_stage+1);
            ++_stage;
            change_state(GAME);
        }catch( std::out_of_range& oor ){
            change_state(END_GAME);
        }
    }else if( new_state == END_GAME ){

    }

}

void Game::load_stage(int new_stage){
    //std::cout << "LOAD STAGE " << new_stage << std::endl;
    _gb = Resources::instance()->board(new_stage);
}
